exports.install = function(Vue, options) {
	// 接口域名,调用this.apiUrl
	// Vue.prototype.apiUrl = 'https://gmyx.119kst.com/prod-api'
	Vue.prototype.apiUrl = 'http://192.168.10.174:8081'
	Vue.prototype.shopCart = {}

	Vue.prototype.addCart = (goods) => {
		var shopCart = Vue.prototype.shopCart
		if (!shopCart[goods.goodsId]) {
			shopCart[goods.goodsId] = {}
			shopCart[goods.goodsId].goods = goods
			shopCart[goods.goodsId].count = 0
		}
		shopCart[goods.goodsId].count = shopCart[goods.goodsId] ? shopCart[goods.goodsId].count + 1 : 1,

			console.log('添加购物车', Vue.prototype.shopCart);
		Vue.prototype.$forceUpdate()
		updateCart(goods.goodsId, shopCart[goods.goodsId].count)
	}

	Vue.prototype.subtractCart = (goods) => {
		var shopCart = Vue.prototype.shopCart
		if (shopCart[goods.goodsId] && shopCart[goods.goodsId].count > 0) {
			shopCart[goods.goodsId].count = shopCart[goods.goodsId].count - 1
			if (shopCart[goods.goodsId].count <= 0) {
				delete shopCart[goods.goodsId]
				updateCart(goods.goodsId, 0)
				console.log('减少购物车', Vue.prototype.shopCart);
				return
			}
			updateCart(goods.goodsId, shopCart[goods.goodsId].count)
		}

		console.log('减少购物车', Vue.prototype.shopCart);

	}

	var updateCart = (goodsId, number) => {
		uni.request({
			url: Vue.prototype.apiUrl + "/client/shopcar/add",
			method: "POST",
			header: {
				'content-type': 'application/x-www-form-urlencoded'
			},
			data: {
				goodsId: goodsId,
				number: number
			},
			success: (res) => {
				const result = res.data;
				console.log("app", result)
			},
			fail: (res) => {
				uni.showToast({
					title: res.errMsg,
					icon: "none"
				});
			}
		})
	}

	Vue.prototype.initCart = () => {
		uni.request({
			url: Vue.prototype.apiUrl + "/client/shopcar/list",
			method: "POST",
			header: {
				'content-type': 'application/x-www-form-urlencoded'
			},
			data: {
				pageNum: 1,
				pageSize: 1000
			},
			success: (res) => {
				const result = res.data;
				if (result.code === 200) {
					Vue.prototype.shopCart = result.data;
				} else {
					uni.showToast({
						title: result.msg,
						icon: "none"
					});
				}
			},
			fail: (res) => {
				uni.showToast({
					title: res.errMsg,
					icon: "none"
				});
			}
		})
	}
};
